package deferx

import (
	"fmt"

	"github.com/gookit/color"
)

// 棧
// 其他-->return --> deferx-->完全退出
type deferBase struct {
}

func (r deferBase) Defer() {
	defer r.defer1()
	color.Redln("Defer")
}
func (r deferBase) defer1() {
	defer r.defer2()
	defer color.Redln("defer1 b")
	color.Redln("defer1 a")
}
func (r deferBase) defer2() {
	color.Redln("defer2")
}

type deferRecover struct {
}

func (r deferRecover) DeferRecoverB() {
	//先入棧，程序結束後自動執行
	defer func() { // 要在panic之前 ，否則無法捕獲錯誤信息
		if ok := recover(); ok != nil {
			fmt.Println("defer recover ok")
		}
	}()
	panic("Defer Panic Recover!")
}
func (r deferRecover) DeferRecoverA() {
	panic("DeferRecover!") //在前面直接中斷程序
	//程序直接中断
	func() {
		if ok := recover(); ok != nil {
			fmt.Println("recover ok")
		}
	}()
}
